/*
 * ShopFragment.java
 * business
 *
 * Created by ChenTao on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.shop;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;

import com.yidiandao.business.R;
import com.yidiandao.business.base.SuperBaseEventFragment;
import com.yidiandao.business.ui.adapter.ShopAdapter;
import com.yidiandao.business.ui.searchabout.searchshopandgoods.SearchActivity;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.view.AbsListViewScrollDetector;
import com.yidiandao.business.utils.AnimatorUtils;
import com.yidiandao.business.utils.SysUtils;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

import in.srain.cube.views.loadmore.LoadMoreContainer;
import in.srain.cube.views.loadmore.LoadMoreHandler;
import in.srain.cube.views.loadmore.LoadMoreListViewContainer;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

/** 商铺列表-View */
public class ShopFragment extends SuperBaseEventFragment implements ShopContract.View {

    private static final String TAG = ShopFragment.class.getSimpleName();

    private ShopContract.Presenter mPresenter;

    private Toolbar mToolbar;

    private ListView mListView;

    private PtrClassicFrameLayout mPtrFrame;

    private ShopAdapter mShopAdapter;

    private LoadMoreListViewContainer mLoadMoreContainer;

    private ImageView mIvToTop;

    private ImageButton mIbtnSearch;

    private boolean isVisibility;

    private boolean isOpening, isHiding;
    AbsListViewScrollDetector scrollDetector = new AbsListViewScrollDetector() {
        @Override
        public void onScrollUp() {
            // 动画隐藏
            hideAnimator(mIvToTop);
        }

        @Override
        public void onScrollDown() {
            if (UIUtils.getListViewScrollY(mListView)
                    > SysUtils.getScreenHeight(UIUtils.getContext())) {
                // 动画显示
                showAnimator(mIvToTop);
            } else {
                // 动画隐藏
                hideAnimator(mIvToTop);
            }
        }
    };

    public static ShopFragment getInstance() {
        return new ShopFragment();
    }

    @Override
    protected void initViews() {
        mToolbar = (Toolbar) mRootView.findViewById(R.id.toolbar);
        mIbtnSearch = (ImageButton) mRootView.findViewById(R.id.ibtn_search);
        mPtrFrame = (PtrClassicFrameLayout) mRootView.findViewById(R.id.ptr_frame);
        mListView = (ListView) mRootView.findViewById(R.id.list_view);
        mLoadMoreContainer =
                (LoadMoreListViewContainer) mRootView.findViewById(R.id.load_more_container);
        mIvToTop = (ImageView) mRootView.findViewById(R.id.iv_to_top);

        mShopAdapter = new ShopAdapter(mActivity);
        mListView.setAdapter(mShopAdapter);

        mToolbar.setNavigationOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mActivity.finish();
                    }
                });
        mIbtnSearch.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        SearchActivity.actionStart(mActivity, 1);
                    }
                });
        initUlptr();
    }

    @Override
    protected void startLoadData() {
        mPresenter.start();
    }

    private void initUlptr() {
        // 下拉刷新
        mPtrFrame.setLastUpdateTimeRelateObject(this);
        mPtrFrame.setPtrHandler(
                new PtrDefaultHandler() {

                    @Override
                    public boolean checkCanDoRefresh(
                            PtrFrameLayout frame, View content, View header) {
                        return PtrDefaultHandler.checkContentCanBePulledDown(
                                frame, mListView, header);
                    }

                    @Override
                    public void onRefreshBegin(PtrFrameLayout frame) {
                        mPresenter.loadShopList(true);
                    }
                });

        // 上拉加载更多
        mLoadMoreContainer.useDefaultFooter();
        mLoadMoreContainer.setLoadMoreHandler(
                new LoadMoreHandler() {
                    @Override
                    public void onLoadMore(LoadMoreContainer loadMoreContainer) {
                        mPresenter.loadShopList(false);
                    }
                });

        mIvToTop.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // 返回顶部
                        smoothToTop();
                    }
                });
        scrollDetector.setListView(mListView);
        mLoadMoreContainer.setOnScrollListener(scrollDetector);
    }

    /** 显示动画 */
    private void showAnimator(View view) {
        AnimatorUtils.showToTopAnimator(
                view,
                isVisibility,
                isOpening,
                new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        isOpening = true;
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        isVisibility = true;
                        isOpening = false;
                    }
                });
    }

    /** 隐藏动画 */
    private void hideAnimator(final View view) {
        AnimatorUtils.hideToTopAnimator(
                view,
                isVisibility,
                isHiding,
                new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        isHiding = true;
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        view.setVisibility(View.GONE);
                        isVisibility = false;
                        isHiding = false;
                    }
                });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_shop;
    }

    @Override
    public void setPresenter(ShopContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return TAG;
    }

    @Override
    public void refreshContent(int result) {
        superRefreshLayoutState(result);
    }

    @Override
    public int checkResultObject(Object resultObject) {
        return superCheckListState(resultObject);
    }

    @Override
    public void setDisplayListItems(List<DisplayListItem> displayListItems) {
        mShopAdapter.setData(displayListItems);
    }

    @Override
    public void setRefreshComplete() {
        mPtrFrame.refreshComplete();
    }

    @Override
    public void showToast(String text) {
        UIUtils.showToast(text);
    }

    @Override
    public void smoothToTop() {
        if (mListView != null) {
            mListView.smoothScrollToPosition(0);
        }
    }

    @Override
    public void loadMoreFinish(boolean emptyResult, boolean hasMore) {
        mLoadMoreContainer.loadMoreFinish(emptyResult, hasMore);
    }

    @Override
    public void loadMoreError() {
        mLoadMoreContainer.loadMoreError(0, UIUtils.getString(R.string.load_more_failed));
    }

    @Override
    public void notifyDataChanged() {
        mPresenter.start();
    }
}
